package cn.cloudreal.cbms.statements.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.statements.service.ResourceStateService;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.ResourceState;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionSupport;

public class ResourceStateAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private ResourceStateService resourceStateService;
	/** 日志工具类实例 */
	private static final Logger log = Logger
			.getLogger(ResourceStateAction.class);

	private List<ResourceState> dataListResourceState = null; // 资源状态累计金额统计表list
	private List<Dictionary> dataListUserCITY = null; // 归属地市

	// 查询条件
	private String userCity; // 归属地市
	private String type; // 判断是否是初次进入
	private String startDate; // 开始日期
	private String endDate; // 结束日期
	/** 分页对象 */
	PageBean page;
	/** 当前页 */
	int currentPage;

	/**
	 * 报表信息列表
	 */
	public String showTable() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log
					.info("Enter ContractListAction showTable() method, return String");
		}

		try {
			initDictionary();
			initCondition();
			getUserSession();
			String test = startDate + endDate;
			// 初始化分页对象
			page = new PageBean(currentPage == 0 ? 1 : currentPage,
					Constants.PAGE_SIZE);
			if (!test.equals("") && test != null) {
				// 查询
				Map condition = new HashMap();
				condition.put("i_startdate", startDate);
				condition.put("i_enddate", endDate);
				condition.put("i_city", userCity);
				dataListResourceState = resourceStateService
						.getResourceStateList(condition, page);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log
					.info("Exit ContractListAction showTable() method, return String");
		}
		return SUCCESS;
	}

	private void initCondition() {
		if (StringUtils.isEmpty(startDate)) {
			startDate = "";
		}
		if (StringUtils.isEmpty(endDate)) {
			endDate = "";
		}
	}

	private void getUserSession() {
		// 获取userSession
		UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
				.getSession().getAttribute("logUser");
		String range = null;
		String cityCode = null;
		range = userSession.getUserRole().getRoleRange() == null ? ""
				: userSession.getUserRole().getRoleRange();
		cityCode = userSession.getUsercity() == null ? "" : userSession
				.getUsercity();
		// 设置权限
		if (null == type) {
			type = "1";
			userCity = cityCode;
		}
		if ("1".equals(range)) {
		} else {
			dataListUserCITY = DatadicContext.getInstance()
					.getDicDataListByCode("CITY");// 归属地市
			dataListUserCITY.clear();
			Dictionary date = DatadicContext.getInstance().getDatadicObject(
					cityCode, "CITY");
			dataListUserCITY.add(date);
			userCity = cityCode;
		}

	}

	private void initDictionary() {
		dataListUserCITY = DatadicContext.getInstance().getDicDataListByCode(
				"CITY");// 归属地市
		if (dataListUserCITY == null) {
			dataListUserCITY = new ArrayList<Dictionary>();
		}

	}

	/**
	 * get And set action
	 */
	public ResourceStateService getResourceStateService() {
		return resourceStateService;
	}

	public void setResourceStateService(
			ResourceStateService resourceStateService) {
		this.resourceStateService = resourceStateService;
	}

	public List<ResourceState> getDataListResourceState() {
		return dataListResourceState;
	}

	public void setDataListResourceState(
			List<ResourceState> dataListResourceState) {
		this.dataListResourceState = dataListResourceState;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public static Logger getLog() {
		return log;
	}

	public List<Dictionary> getDataListUserCITY() {
		return dataListUserCITY;
	}

	public void setDataListUserCITY(List<Dictionary> dataListUserCITY) {
		this.dataListUserCITY = dataListUserCITY;
	}

	public String getUserCity() {
		return userCity;
	}

	public void setUserCity(String userCity) {
		this.userCity = userCity;
	}

}
